package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"time"
)

func Goodtype(ctx *gin.Context) {

	var goodtype []models.Goodtype
	data_source.Db.Find(&goodtype)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    goodtype,
		"msg":     "ok",
	})
	return
}

type GoodsRBody struct {
	Data       []models.Goods `json:"data"`
	PageNo     int            `json:"pageNo"`
	PageSize   int            `json:"pageSize"`
	TotalCount int64          `json:"totalCount"`
	TotalPage  int64          `json:"totalPage"`
}

type GoodsBody struct {
	Page       int    `json:"page"`
	Pagesize   int    `json:"pagesize"`
	Goodtypeid int    `json:"goodtypeid"`
	ID         int64  `json:"id"`
	Uid        int    `json:"uid"`
	Qudao      string `json:"qudao" gorm:"qudao"`
}

func Goods(ctx *gin.Context) {
	request := GoodsBody{}
	ctx.ShouldBind(&request)
	goodtypeid := request.Goodtypeid
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Goods
	res := data_source.Db.Where("status = 1").Where("shangcheng = 2")
	if goodtypeid > 0 {
		res = res.Where("goodtypeid = ?", goodtypeid)
	}
	res.Model(&models.Goods{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := GoodsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Goodrecommend(ctx *gin.Context) {
	request := GoodsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Goods
	res := data_source.Db.Where("status = 1").Where("recommend = 2")

	res.Model(&models.Goods{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := GoodsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
func Goodinfo(ctx *gin.Context) {
	request := GoodsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid

	var good models.Goods
	data_source.Db.Where("id = ?", id).First(&good)

	var collect models.Collect
	data_source.Db.Where("goodid = ?", id).Where("uid = ?", uid).Where("state = 1").First(&collect)
	good.Collect = 1
	if collect.ID > 0 {
		good.Collect = 2
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    good,
		"msg":     "ok",
	})
	return
}
func Goodcollect(ctx *gin.Context) {
	request := GoodsBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	uid := request.Uid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}

	var collect models.Collect
	data_source.Db.Where("goodid = ?", id).Where("uid = ?", uid).Where("state = 1").First(&collect)
	if collect.ID > 0 {
		data_source.Db.Where("goodid = ?", id).Where("uid = ?", uid).Where("state = 1").Delete(&models.Collect{})
	} else {
		var good models.Goods
		data_source.Db.Where("id = ?", id).First(&good)
		collectadd := models.Collect{
			Goodid: int64(id),
			Uid:    int64(uid),
			State:  1,
			Name:   good.Name,
			Image:  good.Image,
		}
		data_source.Db.Create(&collectadd)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type GoodbuyBody struct {
	Goodid    int64  `json:"goodid"`
	Uid       int64  `json:"uid"`
	Addressid int64  `json:"addressid"`
	Paystatus int64  `json:"paystatus"`
	Num       int64  `json:"num"`
	Beizhu    string `json:"beizhu" gorm:"beizhu"`
	Qudao     string `json:"qudao" gorm:"qudao"`
}

func Goodbuy(ctx *gin.Context) {
	request := GoodbuyBody{}
	ctx.ShouldBind(&request)
	goodid := request.Goodid
	uid := request.Uid
	qudao := request.Qudao
	addressid := request.Addressid
	paystatus := request.Paystatus
	num := request.Num
	beizhu := request.Beizhu
	ip := ctx.ClientIP()
	if num == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请传递数量",
		})
		return
	}
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}

	var good models.Goods
	data_source.Db.Where("id = ?", goodid).First(&good)

	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	if user.ID == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "用户不存在",
		})
		return
	}
	if paystatus == 3 {
		if user.Daibi < good.Price*float64(num) {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "矿石不足",
			})
			return
		}
	} else if paystatus == 4 {
		if user.Mojing < good.Mojing*float64(num) {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "魔晶不足",
			})
			return
		}
	}

	orderno := Generateto(time.Time{})
	orderadd := models.Ordergood{
		Uid:       uid,
		Goodid:    goodid,
		Addressid: addressid,
		Orderno:   orderno,
		Addtime:   time.Now().Unix(),
		Status:    1,
		Num:       num,
		Price:     good.Price * float64(num),
		Paystatus: paystatus,
		Beizhu:    beizhu,
		Qudao:     qudao,
	}
	data_source.Db.Create(&orderadd)

	//startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := getTodayMidnightTimestamp()

	if paystatus == 1 {
		aliRsp := Goodalipay(orderno, int64(good.Price*float64(num)*100))

		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    aliRsp,
			"msg":     "ok",
		})
		return
	} else if paystatus == 2 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    orderadd.ID,
			"msg":     "ok",
		})
		return
	} else if paystatus == 3 {
		data_source.Db.Model(&models.Ordergood{}).Where("id = ?", orderadd.ID).Update("status", 2)
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("daibi", user.Daibi-good.Price*float64(num))
		now := time.Now()
		zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
		var address models.Address
		data_source.Db.Where("id = ?", addressid).First(&address)
		usergoodarea := models.Usergoodarea{
			Uid:       uid,
			Addressid: addressid,
			Phone:     address.Phone,
			Name:      address.Name,
			Province:  address.Province,
			City:      address.City,
			County:    address.County,
			Area:      address.Area,
			Addtime:   zeroToday,
			Kdstatus:  1,
		}
		data_source.Db.Create(&usergoodarea)
		for i := 0; i < int(num); i++ {
			service.Gooduser(user, good, 1, 1, 2, usergoodarea.ID, orderadd.ID)
		}
		var userdayprice models.Userdayprice
		data_source.Db.Where("uid = ?", uid).Where("addtime = ?", startTimestamp).First(&userdayprice)
		if userdayprice.ID == 0 {
			userdaypriceadd := models.Userdayprice{
				Uid:     uid,
				Addtime: startTimestamp,
				Price:   good.Price * float64(num),
			}
			data_source.Db.Create(&userdaypriceadd)
		} else {
			data_source.Db.Model(&models.Userdayprice{}).Where("id = ?", userdayprice.ID).Update("price", userdayprice.Price+good.Price*float64(num))
		}

		userdaibi := models.Userdaibi{
			Uid:     uid,
			Status:  2,
			Addtime: time.Now().Unix(),
			Daibi:   good.Price * float64(num),
			Title:   "购买商品",
			State:   5,
			Sydaibi: user.Daibi - good.Price*float64(num),
		}
		data_source.Db.Create(&userdaibi)

		userorder := models.Userorder{
			Uid:       uid,
			Addtime:   time.Now().Unix(),
			State:     1,
			Paystatus: paystatus,
			Price:     good.Price * float64(num),
			Orderid:   orderadd.ID,
		}
		data_source.Db.Create(&userorder)

		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    orderadd.ID,
			"msg":     "ok",
		})
		return
	} else if paystatus == 4 {
		data_source.Db.Model(&models.Ordergood{}).Where("id = ?", orderadd.ID).Update("status", 2)
		data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("mojing", user.Mojing-good.Mojing*float64(num))
		now := time.Now()
		zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
		var address models.Address
		data_source.Db.Where("id = ?", addressid).First(&address)
		usergoodarea := models.Usergoodarea{
			Uid:       uid,
			Addressid: addressid,
			Phone:     address.Phone,
			Name:      address.Name,
			Province:  address.Province,
			City:      address.City,
			County:    address.County,
			Area:      address.Area,
			Addtime:   zeroToday,
			Kdstatus:  1,
		}
		data_source.Db.Create(&usergoodarea)
		service.Gooduser(user, good, num, 1, 4, usergoodarea.ID, orderadd.ID)
		userorder := models.Userorder{
			Uid:       uid,
			Addtime:   time.Now().Unix(),
			State:     1,
			Paystatus: paystatus,
			Price:     good.Price * float64(num),
			Orderid:   orderadd.ID,
		}
		data_source.Db.Create(&userorder)

		usermojing := models.Usermojing{
			Uid:      uid,
			Addtime:  time.Now().Unix(),
			State:    2,
			Title:    good.Name,
			Num:      num,
			Mojing:   good.Mojing * float64(num),
			Symojing: user.Mojing + good.Mojing*float64(num),
		}
		data_source.Db.Create(&usermojing)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    orderadd.ID,
			"msg":     "ok",
		})
		return
	} else if paystatus == 5 {

		data := Wechatpayxcxgood(orderno, good.Price*float64(num), user, ip, orderadd.ID)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    data,
			"msg":     "ok",
		})
		return
	}

}

type UsergoodsRBody struct {
	Daytime int64             `json:"daytime" gorm:"daytime"`
	Goods   []models.Usergood `json:"goods" gorm:"goods"`
}
type UsergoodszongRBody struct {
	Goodnum        int64            `json:"goodnum" gorm:"goodnum"`
	UsergoodsRBody []UsergoodsRBody `json:"usergoodsRBody" gorm:"usergoodsRBody"`
}

func Usergoods(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	state := request.State
	status := request.Status
	shouhuo := request.Shouhuo

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var good []models.Usergood
	res := data_source.Db.Where("uid = ?", uid).Where("status < 6")
	if status > 0 {
		res = res.Where("status = ?", status)
		if status == 3 {
			if shouhuo > 0 {
				res = res.Where("shouhuo = ?", shouhuo)
			}
		}
	}
	if state > 0 {
		res = res.Where("state = ?", state)
	}

	res.Order("id desc").Find(&good)
	goodnum := len(good)
	var daytimearr []int64
	for _, g := range good {
		cunzai := 1
		for _, d := range daytimearr {
			if g.Addtime == d {
				cunzai = 2
			}
		}
		if cunzai == 1 {
			daytimearr = append(daytimearr, g.Addtime)
		}
	}

	var usergoodsRBodys []UsergoodsRBody
	for _, d := range daytimearr {
		var goods []models.Usergood
		for _, g := range good {
			if d == g.Addtime {
				goods = append(goods, g)
			}
		}
		usergoodsRBody := UsergoodsRBody{
			Daytime: d,
			Goods:   goods,
		}
		usergoodsRBodys = append(usergoodsRBodys, usergoodsRBody)
	}
	data := UsergoodszongRBody{
		UsergoodsRBody: usergoodsRBodys,
		Goodnum:        int64(goodnum),
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type UsergoodsBody struct {
	Uid         int64  `json:"uid" gorm:"uid"`
	Usergoodids string `json:"usergoodids" gorm:"usergoodids"`
	Usergoodid  int64  `json:"usergoodid" gorm:"usergoodid"`
	Addressid   int64  `json:"addressid" gorm:"addressid"`
	State       int64  `json:"state" gorm:"state"`     // 1直接购买 2扭蛋机 3一番赏 4魔晶兑换
	Status      int64  `json:"status" gorm:"status"`   // 1待确认 2待发货 3已发货4已锁定 5已分解
	Shouhuo     int64  `json:"shouhuo" gorm:"shouhuo"` //0全部 1运输中 2已收货 3未发货
	Beizhu      string `json:"beizhu" gorm:"beizhu"`
	Paystatus   int64  `json:"paystatus" gorm:"paystatus"`
	Qudao       string `json:"qudao" gorm:"qudao"`
}

func Usergoodfh(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids
	addressid := request.Addressid
	beizhu := request.Beizhu
	paystatus := request.Paystatus
	qudao := request.Qudao

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	goodidarr := strings.Split(usergoodids, ",")
	var goods []models.Usergood
	data_source.Db.Where("id in (?)", goodidarr).Find(&goods)

	var goodprice float64
	for _, g := range goods {
		goodprice = goodprice + g.Goodprice
		if g.Status != 1 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择待确认状态商品",
			})
			return
		}
	}

	var kdstatus int64
	var kdprice float64
	if goodprice >= 100 {
		kdstatus = 1
		kdprice = 0
	} else {
		kdstatus = 10
		kdprice = 15
		if paystatus == 0 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择支付方式",
			})
			return
		}

	}

	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
	var address models.Address
	data_source.Db.Where("id = ?", addressid).First(&address)
	orderno := Generateto(time.Time{})
	usergoodarea := models.Usergoodarea{
		Uid:       uid,
		Addressid: addressid,
		Phone:     address.Phone,
		Name:      address.Name,
		Province:  address.Province,
		City:      address.City,
		County:    address.County,
		Area:      address.Area,
		Addtime:   zeroToday,
		Kdstatus:  kdstatus,
		Beizhu:    beizhu,
		Goodids:   usergoodids,
		Orderno:   orderno,
		Kdprice:   kdprice,
		Qudao:     qudao,
	}
	data_source.Db.Create(&usergoodarea)

	if kdstatus == 1 {
		usergoodupdate := models.Usergood{
			Usergoodareaid: usergoodarea.ID,
			Shouhuo:        3,
			Status:         2,
			Updatetime:     time.Now().Unix(),
		}
		data_source.Db.Where("id in (?)", goodidarr).Updates(&usergoodupdate)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    "ok",
			"msg":     "ok",
		})
		return
	} else {

		ip := ctx.ClientIP()
		if paystatus == 1 {

		} else if paystatus == 2 {

		} else if paystatus == 3 {
			var user models.User
			data_source.Db.Where("id = ?", uid).First(&user)
			data := Wechatpayxcxyoufei(orderno, 15, user, ip)
			ctx.JSON(http.StatusOK, gin.H{
				"code":    0,
				"error":   0,
				"success": 210,
				"data":    data,
				"msg":     "ok",
			})
			return
		}
	}

}

type UsergoodfjseeRBody struct {
	Goodname  string  `json:"goodname" gorm:"goodname"`
	Goodimage string  `json:"goodimage" gorm:"goodimage"`
	Num       int64   `json:"num" gorm:"num"`
	Mojing    float64 `json:"mojing" gorm:"mojing"`
	Price     float64 `json:"price" gorm:"price"`
	State     int64   `json:"state" gorm:"state"` // 1直接购买 2扭蛋机 3一番赏 4魔晶兑换
}

func Usergoodfjsee(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	gooduseridarr := strings.Split(usergoodids, ",")
	var usergoods []models.Usergood
	data_source.Db.Where("id in (?)", gooduseridarr).Find(&usergoods)
	var goodidarr []int64
	for _, g := range usergoods {
		goodidarr = append(goodidarr, g.Goodid)
		if g.Status != 1 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择待确认状态商品",
			})
			return
		}
	}
	var goods []models.Goods
	data_source.Db.Where("id in (?)", goodidarr).Find(&goods)
	var usergoodfjseeRBodys []UsergoodfjseeRBody
	for _, g := range goods {
		for _, u := range usergoods {
			if g.ID == u.Goodid {
				usergoodfjseeRBody := UsergoodfjseeRBody{
					Goodname:  g.Name,
					Goodimage: g.Image,
					Mojing:    g.Recoveryprice,
					Price:     g.Price,
					Num:       u.Num,
					State:     u.State,
				}
				usergoodfjseeRBodys = append(usergoodfjseeRBodys, usergoodfjseeRBody)
			}
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    usergoodfjseeRBodys,
		"msg":     "ok",
	})
	return
}

func Usergoodfj(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	gooduseridarr := strings.Split(usergoodids, ",")
	var usergoods []models.Usergood
	data_source.Db.Where("id in (?)", gooduseridarr).Find(&usergoods)
	var goodidarr []int64
	for _, g := range usergoods {
		goodidarr = append(goodidarr, g.Goodid)
		if g.Status != 1 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择待确认状态商品",
			})
			return
		}
	}
	var goods []models.Goods
	data_source.Db.Where("id in (?)", goodidarr).Find(&goods)
	var mojing float64
	for _, g := range goods {
		for _, u := range usergoods {
			if g.ID == u.Goodid {
				mojing = mojing + g.Recoveryprice*float64(u.Num)
			}
		}
	}
	data_source.Db.Model(&models.Usergood{}).Where("id in (?)", gooduseridarr).Update("status", 5)
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	data_source.Db.Model(&models.User{}).Where("id = ?", uid).Update("mojing", user.Mojing+mojing)

	usermojing := models.Usermojing{
		Uid:      uid,
		Addtime:  time.Now().Unix(),
		State:    1,
		Title:    "赏品分解",
		Num:      int64(len(gooduseridarr)),
		Mojing:   mojing,
		Symojing: user.Mojing + mojing,
	}
	data_source.Db.Create(&usermojing)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Usergoodsd(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	gooduseridarr := strings.Split(usergoodids, ",")
	var usergoods []models.Usergood
	data_source.Db.Where("id in (?)", gooduseridarr).Find(&usergoods)
	var goodidarr []int64
	for _, g := range usergoods {
		goodidarr = append(goodidarr, g.Goodid)
		if g.Status != 1 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择待确认状态商品",
			})
			return
		}
	}
	data_source.Db.Model(&models.Usergood{}).Where("id in (?)", gooduseridarr).Update("status", 4)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Usergoodjs(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	gooduseridarr := strings.Split(usergoodids, ",")
	var usergoods []models.Usergood
	data_source.Db.Where("id in (?)", gooduseridarr).Find(&usergoods)
	var goodidarr []int64
	for _, g := range usergoods {
		goodidarr = append(goodidarr, g.Goodid)
		if g.Status != 4 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择已锁定状态商品",
			})
			return
		}
	}
	data_source.Db.Model(&models.Usergood{}).Where("id in (?)", gooduseridarr).Update("status", 1)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Usergoodqxfh(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodids := request.Usergoodids

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	gooduseridarr := strings.Split(usergoodids, ",")

	var usergoods []models.Usergood
	data_source.Db.Where("id in (?)", gooduseridarr).Find(&usergoods)

	var goodidarr []int64
	for _, g := range usergoods {
		goodidarr = append(goodidarr, g.Goodid)
		if g.Status != 2 {
			ctx.JSON(http.StatusOK, gin.H{
				"code":    400,
				"error":   1,
				"success": 400,
				"data":    "on",
				"msg":     "请选择已申请状态商品",
			})
			return
		}
	}

	data_source.Db.Model(&models.Usergood{}).Where("id in (?)", gooduseridarr).Update("status", 1)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type UsergoodkdRBody struct {
	Data     []models.Kuaidi     `json:"data"`
	Userarea models.Usergoodarea `json:"userarea"`
	Kdorder  string              `json:"kdorder" gorm:"kdorder"`
	Kuaidi   string              `json:"kuaidi" gorm:"kuaidi"`
}

func Usergoodkd(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodid := request.Usergoodid
	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}

	var usergood models.Usergood
	data_source.Db.Where("id = ?", usergoodid).First(&usergood)

	var userkuaidi models.Usergoodarea
	data_source.Db.Where("id = ?", usergood.Usergoodareaid).First(&userkuaidi)
	var kuaidi []models.Kuaidi
	data_source.Db.Where("usergoodid = ?", usergoodid).Order("id desc").Find(&kuaidi)
	data := UsergoodkdRBody{
		Data:     kuaidi,
		Userarea: userkuaidi,
		Kdorder:  usergood.Kdorder,
		Kuaidi:   usergood.Kuaidi,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Usergoodxj(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodid := request.Usergoodid

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var usergood models.Usergood
	data_source.Db.Where("id = ?", usergoodid).First(&usergood)
	if usergood.Status != 5 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "只有已分解商品才可以下架",
		})
		return
	}
	data_source.Db.Model(&models.Usergood{}).Where("id = ?", usergoodid).Update("status", 6)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Usergoodsh(ctx *gin.Context) {
	request := UsergoodsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	usergoodid := request.Usergoodid

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var usergood models.Usergood
	data_source.Db.Where("id = ?", usergoodid).First(&usergood)
	if usergood.Status != 3 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "只有已发货才可以收货",
		})
		return
	}
	if usergood.Shouhuo == 2 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请勿重复收货",
		})
		return
	}
	data_source.Db.Model(&models.Usergood{}).Where("id = ?", usergoodid).Update("shouhuo", 2)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
